package policy.stop;


import java.util.ArrayList;
import java.util.HashSet;


import distributedRE.model.DistributedREModel;
import distributedRE.model.Document;
import distributedRE.model.Session;


public class TTuplesExtracted implements StopPolicy {

	private long T;

	public TTuplesExtracted(long T){
		
		this.T = T;
	
	}
	
//	public boolean finished(DistributedREModel dREModel) {
//		
//		NewTuplesExtracted nTE = new NewTuplesExtracted();
//		
//		int tuples = 0;
//		
//		for (int i = 1; i <= dREModel.getNumberOfSessions(); i++) {
//			
//			tuples += (int)(nTE.getLastSessionStatistic(dREModel.getModelatSession(i)).doubleValue());
//			
//			System.out.println("TTuplesExtracted: " + tuples);
//			
//			if (tuples >= T)
//				return true;
//			
//		}
//		
//		return false;
//		
//	}

	public String toString(){
		return new String(T + " Tuples Extracted");
	}

	public boolean finished(DistributedREModel dREModel) {
		
		ArrayList<Session> sessions = dREModel.getAllSessions();
		
		HashSet<Document> documents = new HashSet<Document>();
		
		long tuples = 0;
		
		for (Session session : sessions) {
		
			ArrayList<ArrayList<Document>> doclists = session.getDocumentLists();
			
			for (ArrayList<Document> arrayList : doclists) {
				
				for (Document document : arrayList) {
					
					documents.add(document);
					
				}
				
			}
			
		}
		
		for (Document document : documents) {
			
			tuples += document.getTuples(dREModel.getInformationExtractor().getId(), dREModel.getRelation().getId()).size();
			
		}

		return (tuples>=T);
		
		
	}
	
}